﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OUSMS.AppLogic.DataAccessLayer;
using OUSMS.AppLogic.BusinessLogicLayer;
using System.Web.ModelBinding;
using OUSMS.Helpers;
namespace OUSMS
{
    public partial class EndMotion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // id 参数应与控件上设置的 DataKeyNames 值匹配
        // 或用值提供程序特性装饰，例如 [QueryString]int id
        public Motions motionDetailFormView_GetItem([QueryString("MID")]long id)
        {
            MotionsBLL motionsBLL = new MotionsBLL();
            return motionsBLL.GetMotionsByID(id);
        }

        protected void cancelBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("WaitForEndMotionList.aspx");
        }

        protected void filesGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label indexLbl = e.Row.FindControl("indexLbl") as Label;
                indexLbl.Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        // 返回类型可以更改为 IEnumerable，但是为了支持
        // 分页和排序，必须添加以下参数:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<MotionFiles> filesGridView_GetData([QueryString("MID")]long id)
        {
            MotionFilesBLL motionFilesBLL = new MotionFilesBLL();
            return motionFilesBLL.GetMotionFilesByMotionID(id);
        }

        protected void motionDetailFormView_PreRender(object sender, EventArgs e)
        {
            Repeater feedbackRepeater = motionDetailFormView.FindControl("feedbackRepeater") as Repeater;
            MotionReviewerCommentBLL motionReviewerCommentBLL = new MotionReviewerCommentBLL();
            feedbackRepeater.DataSource = motionReviewerCommentBLL.GetMotionReviewerCommentByMotionID(Convert.ToInt64(Request.QueryString["MID"]));
            feedbackRepeater.DataBind();
        }

        protected void okBtn_Click(object sender, EventArgs e)
        {
            Label errorMsgLbl = motionDetailFormView.FindControl("errorMsgLbl") as Label;
            try
            {
                TextBox feedbackTxtBox = motionDetailFormView.FindControl("feedbackTxtBox") as TextBox;
                if (!string.IsNullOrEmpty(feedbackTxtBox.Text.Trim()))
                {
                    UsersBLL userBLL = new UsersBLL();
                    Users user = userBLL.GetUserByLoginName(User.Identity.Name);

                    MotionsBLL motionsBLL = new MotionsBLL();
                    Motions motions = motionsBLL.GetMotionsByID(Convert.ToInt64(Request.QueryString["MID"]));

                    motions.EndComment = feedbackTxtBox.Text.Trim();
                    motions.EndBy = user.ID;
                    motions.EndDateTime = DateTime.Now;
                    motions.Status = "已立项";
                    motionsBLL.Save();

                    Response.Redirect("WaitForEndMotionList.aspx");
                }
                else
                {
                    errorMsgLbl.Text = string.Format("请填写终审意见");
                    errorMsgLbl.Visible = true;
                }  
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                errorMsgLbl.Text = string.Format("信息保存失败，失败原因：{0}", ex.Message);
                errorMsgLbl.Visible = true;
            }
        }

        protected void notBtn_Click(object sender, EventArgs e)
        {
            Label errorMsgLbl = motionDetailFormView.FindControl("errorMsgLbl") as Label;
            try
            {
                TextBox feedbackTxtBox = motionDetailFormView.FindControl("feedbackTxtBox") as TextBox;

                if (!string.IsNullOrEmpty(feedbackTxtBox.Text.Trim()))
                {
                    UsersBLL userBLL = new UsersBLL();
                    Users user = userBLL.GetUserByLoginName(User.Identity.Name);

                    MotionsBLL motionsBLL = new MotionsBLL();
                    Motions motions = motionsBLL.GetMotionsByID(Convert.ToInt64(Request.QueryString["MID"]));

                    motions.EndComment = feedbackTxtBox.Text.Trim();
                    motions.EndBy = user.ID;
                    motions.EndDateTime = DateTime.Now;
                    motions.Status = "不立项";
                    motionsBLL.Save();

                    Response.Redirect("WaitForEndMotionList.aspx");
                }
                else
                {
                    errorMsgLbl.Text = string.Format("请填写终审意见");
                    errorMsgLbl.Visible = true;
                }                
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
             
                errorMsgLbl.Text = string.Format("信息保存失败，失败原因：{0}", ex.Message);
                errorMsgLbl.Visible = true;
            }
        }
    }
}